package com.shangpintechnology.sci;

import javax.swing.DefaultListSelectionModel;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableModel;

import com.shangpintechnology.sci.cam.MainFrame;
import com.shangpintechnology.sci.machinegenerat.ConfirmHandOutCard;
import com.shangpintechnology.sci.machinegenerat.ConfirmUserInfo;
import com.shangpintechnology.sci.view.ArrangeWaiter;
import com.shangpintechnology.sci.view.View;
public class MainClass implements ListSelectionListener, Runnable {
	private View view;
	private DataModel dataModel;
	private TableModel tableModel;
	private String currentVipCode;
	private ConfirmHandOutCard cinfo;
	public ConfirmUserInfo uinfo;
	private SearchVip sv ;
	private boolean lock;
	public boolean handOutCard;
	public ArrangeWaiter waiterpane ;
	public  TableModel  sharedTodayTableModel;
	public ArrangeWaiter getWaiterpane() {
		return waiterpane;
	}
	private static RifdEventRunnable rfidRunnable;
	public void setWaiterpane(ArrangeWaiter waiterpane) {
		this.waiterpane = waiterpane;
	}
	private static  MainClass singleton;
	public static MainClass getSingleton(){
		if (singleton==null){
			singleton = new MainClass();
		}
		return singleton;
	}
	public static void setSingleton(MainClass sgt){
		singleton =sgt;
	}
	public MainClass() {
		super();
		view = new View();
		dataModel = new DataModel();
		tableModel =dataModel.getTodayVipTable();
		sharedTodayTableModel = tableModel	;
	    lock = false;
	  //添加视频 
/*
		MainFrame mainFrame = new MainFrame();
		mainFrame.initCam();
		view.getLiveImage().add(MainFrame.getProcessor(MainFrame.getCamSource()).getVisualComponent());
		view.getLiveImage().add(mainFrame.getNorthPanel());

*/
	    view.getJTable1().setModel(tableModel);
		view.getJTable1().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		view.getJTable1().getSelectionModel().addListSelectionListener(this);
	}	
	
	public ConfirmUserInfo getUinfo() {
		if(uinfo==null){
			uinfo = new ConfirmUserInfo(view, false);

			uinfo.setLocation(300, 100);
		}
		return uinfo;
	}
	public void setUinfo(ConfirmUserInfo uinfo) {
		this.uinfo = uinfo;
	}
	public ConfirmHandOutCard getCinfo() {
		if(cinfo==null){
			cinfo = new ConfirmHandOutCard(view, false);
		}
		return cinfo;
	}
	public void setCinfo(ConfirmHandOutCard cinfo) {
		this.cinfo = cinfo;
	}
	public SearchVip getSv() {
		return sv;
	}
	public void setSv(SearchVip sv) {
		this.sv = sv;
	}
	

	public static void main(String[] args) throws InterruptedException {
		MainClass main = new MainClass();
		MainClass.setSingleton(main);
		System.out.println(System.getProperties().get("java.library.path")); 

		Thread mainThread = new Thread(main);
		 rfidRunnable = new RifdEventRunnable();
		Thread rfidThread  = new Thread(rfidRunnable);
		
		rfidThread.setPriority(Thread.MAX_PRIORITY);
		
		rfidThread.start();
		mainThread.start();
	}

	public View getView() {
		return view;
	}
	public void setView(View view_) {
		view = view_;
	}
	public void valueChanged(ListSelectionEvent el) {
		 
		if(lock){
			return;
		}
	
		DefaultListSelectionModel tableSelectModel = (DefaultListSelectionModel)el.getSource();
		int row = tableSelectModel.getMinSelectionIndex(); 
		
		JTextField tf1 = view.getJTextField1();
		//JTextField tf2 = view.getJTextField2();
		
		String name = (String) (getView().getJTable1().getModel()).getValueAt(row,1);
		tf1.setText("用户:" + name+ "的标准照片");
		String dir = "D:\\WorkspaceMyEclipse\\SCI-Demo\\src\\com\\shangpintechnology\\sci\\img\\";
		ImageIcon standardImg =new ImageIcon(dir+name+".jpg");
		view.getStandardImage().setIcon(standardImg);
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("用户:");
		stringBuilder.append(name);
		stringBuilder.append("的现场影像");
		//tf2.setText(stringBuilder.toString());
		currentVipCode = (String) view.getJTable1().getValueAt(row, 0);
		TableModel  curVipHistoryModel= dataModel.getCurrentVipHistoryTable(currentVipCode);
		 view.getServiceHistory().setModel(curVipHistoryModel);
		
		AccountInfo accountInfo = dataModel.getVip(currentVipCode);
		if( null==accountInfo){
			JOptionPane.showConfirmDialog(view, "未找到数据");
		}
		else{
		view.getVIPcode().setText(accountInfo.getVipCode());
		view.getUserName().setText( accountInfo.getVipName());
		view.getVIPlevel().setText(accountInfo.getVipLevel());
		view.getSex().setText(accountInfo.getSex());
		view.getAge().setText(accountInfo.getAge());
		view.getRiskStage().setText(accountInfo.getRiskStage());
		view.getBankCode().setText(accountInfo.getBankCode());
		view.getUid().setText(accountInfo.getPassNum());
		}
		DAO dao = new DAO();
		String time = dao.getHistoryTime(currentVipCode);
		view.getJLabelTimeIn().setText("进入时间："+time); 
		
	}
	public String SVRID;
	public boolean newVip;
	public DataModel getDataModel() {
		return dataModel;
	}
	public void setDataModel(DataModel dataModel_) {
		dataModel = dataModel_;
	}
	public boolean isLock() {
		return lock;
	}
	public void setLock(boolean lock_) {
		lock = lock_;
	}
	 @Override
	public void run() {
		 JTable jtable1 = this.view.getJTable1();
		 jtable1 .setModel(sharedTodayTableModel);
		/*if(jtable1.getColumnCount()>0){
			this.view.getJTable1().setRowSelectionInterval(0, 0); 
				
		}*/
		//TODO 这里的保持首行被选中状态不起作用
	}
	public TableModel getSharedTodayTableModel() {
		return sharedTodayTableModel;
	}
	public void setSharedTodayTableModel(TableModel sharedTodayTableModel) {
		this.sharedTodayTableModel = sharedTodayTableModel;
	}

}
